package com.sx.pipeline.ui.setting.modify

import android.text.InputType
import android.view.View
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.SmsCodeType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActChangeLoginPassBinding
import com.sx.pipeline.utils.AndroidUtil
import com.sx.pipeline.utils.CacheDataManager


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 修改登录密码
 */
@Route(path = RoutePath.Setting.MODIFY_LOGIN_PASS)
class ModifyLoginPassActivity :
    CYDataBindingBaseActivity<ActChangeLoginPassBinding, CYBaseViewModel>(R.layout.act_change_login_pass),
    View.OnClickListener {
    @JvmField
    @Autowired(name = "type")
    var type: Int = 0

    private var phone: String = ""
    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }

        when (type) {
            0 -> {
                mBinding.titleBar.title = getString(R.string.modify_login_pass)
            }
            1 -> {
                mBinding.titleBar.title = getString(R.string.modify_pay_pass)
            }
        }
        phone = mViewModel.mModel.getUserData()?.phone.toString()
        mBinding.etPhone.text =
            mViewModel.mModel.getUserData()?.let { AndroidUtil.phoneHide(it.phone) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.smsCodeEvent) {
            mBinding.tvCountDown.start()
        }

        observe(mViewModel.modifyEvent) {
            when (type) {
                0 -> {
                    //修改完登录密码 退出登录
                    mViewModel.mModel.clearToken()
                    RouteManager.Login.intentToLogin(this, object : NavCallback() {
                        override fun onArrival(postcard: Postcard?) {
                            CacheDataManager.clearAllCache(this@ModifyLoginPassActivity)
                            ActivityUtils.finishAllActivities()
                        }
                    })
                }
                1 -> {
                    finish()
                }
            }

        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //获取验证码
            R.id.tv_count_down -> {
                mViewModel.sendSmsCode(phone, SmsCodeType.FORGOT_PWD_SMS.code)
            }
            R.id.tv_submit -> {
                val smsCode = mBinding.etCode.text.toString()
                val pwd = mBinding.editNewPass.text.toString()
                val confirmPwd = mBinding.editConfirmPass.text.toString()
                if (confirmPwd != pwd) {
                    ToastUtils.showShort("两次密码输入不一致，请重新输入")
                    return
                }
                mViewModel.modifyPwd(type, phone, smsCode, confirmPwd)
            }
        }
    }

}